package gpl.scotlandyard.ui.dialogs;

import gpl.scotlandyard.Main;
import gpl.scotlandyard.ui.tools.ToolsFactory;
import gpl.scotlandyard.utils.Config;
import gpl.scotlandyard.utils.I18n;
import gpl.scotlandyard.utils.ImageProvider;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

/**
 * JDialog showing information about ScotlandYard (version, environnement).
 * @author Norbert Martin
 */
public abstract class AboutDialog {

  private static final Insets FULL = new Insets(10, 5, 5, 5);
  private static final Insets NO_TOP = new Insets(0, 5, 5, 5);

  private final static JDialog SINGLETON = new JDialog();
  private static boolean isInit;

  /** Formats text as follow : first arg in bold font, second in standard font.
   * @param bold
   * @param body
   * @return formatted string
   */
  private static String htmlFormat(String bold, String body) {
    return String.format("<html><body><b>%s</b>%s</body></html>", bold, body);
  }

  /**
   * Shows the JDialog.
   * @param parent
   */
  public static void show(Component parent) {
    if (!isInit) {
      init();
    }
    SINGLETON.setLocationRelativeTo(parent);
    SINGLETON.setVisible(true);
  }

  /**
   * Initializes content.
   */
  private static void init() {
    ToolsFactory.setEscapeClosable(SINGLETON, new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        SINGLETON.setVisible(false);
      }
    });
    SINGLETON.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    SINGLETON.setLayout(new BorderLayout());
    SINGLETON.setModal(true);
    SINGLETON.getContentPane().setBackground(Color.WHITE);
    SINGLETON.setTitle(I18n.get("ABOUT_SY"));

    JLabel left = new JLabel(ImageProvider.get(Config.get("ABOUT_IMG")));
    SINGLETON.getContentPane().add(left, BorderLayout.WEST);

    JPanel north = new JPanel(new BorderLayout());
    north.setOpaque(false);
    SINGLETON.getContentPane().add(north, BorderLayout.CENTER);

    JPanel panel = new JPanel(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    gbc.anchor = GridBagConstraints.WEST;
    gbc.insets = FULL;
    north.add(panel, BorderLayout.NORTH);
    panel.setOpaque(false);
    panel.add(new JLabel("<html><body><font color='#1f60a9' size='20'><b>" + I18n.get("SY") + "</b></font></body></html>"), gbc);

    gbc.insets = NO_TOP;
    panel.add(new JLabel(htmlFormat(I18n.get("ABOUT_VERSION"), Main.VERSION)), gbc);

    gbc.insets = NO_TOP;
    panel.add(new JLabel(I18n.get("ABOUT_RIGHT")), gbc);

    SINGLETON.pack();
    SINGLETON.setLocationRelativeTo(null);
  }
}
